package com.sugar.follow.up.module.business.controller.admin.questionnairequestionrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.follow.up.module.business.controller.admin.questionnairequestionrecord.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.questionnairequestionrecord.QuestionnaireQuestionRecordDO;
import com.sugar.follow.up.module.business.service.questionnairequestionrecord.QuestionnaireQuestionRecordService;

@Tag(name = "管理后台 - 问卷发起答题记录")
@RestController
@RequestMapping("/business/questionnaire-question-record")
@Validated
public class QuestionnaireQuestionRecordController {

    @Resource
    private QuestionnaireQuestionRecordService questionnaireQuestionRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建问卷发起答题记录")
    @PreAuthorize("@ss.hasPermission('business:questionnaire-record:create')")
    public CommonResult<Long> createQuestionnaireQuestionRecord(@Valid @RequestBody QuestionnaireQuestionRecordSaveReqVO createReqVO) {
        return success(questionnaireQuestionRecordService.createQuestionnaireQuestionRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新问卷发起答题记录")
    @PreAuthorize("@ss.hasPermission('business:questionnaire-record:update')")
    public CommonResult<Boolean> updateQuestionnaireQuestionRecord(@Valid @RequestBody QuestionnaireQuestionRecordSaveReqVO updateReqVO) {
        questionnaireQuestionRecordService.updateQuestionnaireQuestionRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除问卷发起答题记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:questionnaire-record:delete')")
    public CommonResult<Boolean> deleteQuestionnaireQuestionRecord(@RequestParam("id") Long id) {
        questionnaireQuestionRecordService.deleteQuestionnaireQuestionRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除问卷发起答题记录")
                @PreAuthorize("@ss.hasPermission('business:questionnaire-record:delete')")
    public CommonResult<Boolean> deleteQuestionnaireQuestionRecordList(@RequestParam("ids") List<Long> ids) {
        questionnaireQuestionRecordService.deleteQuestionnaireQuestionRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得问卷发起答题记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:questionnaire-record:query')")
    public CommonResult<QuestionnaireQuestionRecordRespVO> getQuestionnaireQuestionRecord(@RequestParam("id") Long id) {
        QuestionnaireQuestionRecordDO questionnaireQuestionRecord = questionnaireQuestionRecordService.getQuestionnaireQuestionRecord(id);
        return success(BeanUtils.toBean(questionnaireQuestionRecord, QuestionnaireQuestionRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得问卷发起答题记录分页")
    @PreAuthorize("@ss.hasPermission('business:questionnaire-record:query')")
    public CommonResult<PageResult<QuestionnaireQuestionRecordRespVO>> getQuestionnaireQuestionRecordPage(@Valid QuestionnaireQuestionRecordPageReqVO pageReqVO) {
        PageResult<QuestionnaireQuestionRecordDO> pageResult = questionnaireQuestionRecordService.getQuestionnaireQuestionRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QuestionnaireQuestionRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出问卷发起答题记录 Excel")
    @PreAuthorize("@ss.hasPermission('business:questionnaire-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQuestionnaireQuestionRecordExcel(@Valid QuestionnaireQuestionRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QuestionnaireQuestionRecordDO> list = questionnaireQuestionRecordService.getQuestionnaireQuestionRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "问卷发起答题记录.xls", "数据", QuestionnaireQuestionRecordRespVO.class,
                        BeanUtils.toBean(list, QuestionnaireQuestionRecordRespVO.class));
    }

}